programming4us
           
 
 
SQL Server

Processing and Storing Data in SQL Server 2005 : Updating the FileWorker Class

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
12/24/2011 3:34:25 PM
To make the additions useful, we have to update the FileWorker class to read and insert the records into our Tutorials database.

Updating <FileWorker.ProcessFiles>

We need to create an instance of our LINQSQL class and then use the file that we found while polling our process folder to populate a new user record for each line in the process file. To do this we will update the <ProcessFiles> method, which is where we query the outgoing folder, send an e-mail, and then move the file to our processed folder. Listing 1 shows the changes required for <ProcessFiles>.

Listing 1. FileWorker updated ProcessFiles method to support the LINQSQL class implementation.
Private Sub ProcessFiles()
Dim LinqSql As LINQSQL = New LINQSQL()
While Not m_ThreadAction.StopThread
If Not m_ThreadAction.Pause Then
Try
For Each TextFile As String In My.Computer.FileSystem.GetFiles( _
m_FileWorkerOptions.Output, _
FileIO.SearchOption.SearchTopLevelOnly, _
m_FileWorkerOptions.FileType)
If m_ThreadAction.Pause Or m_ThreadAction.StopThread Then
Exit For
End If
Try
Dim ProcessFile As String = _
m_FileWorkerOptions.ProcessedPath("\" + _
My.Computer.FileSystem.GetName(TextFile))
'File is moved so lets read it out of the Output Folder
If (Me.MailEnabled) Then
Dim message As String
Try
message = "Processing File Data:[" + _
My.Computer.FileSystem.ReadAllText(TextFile) + _
"]] From File - "
Catch ex As Exception
message = "Unable to read from file - "
End Try
'Send the Email and then move it again to the processed Folder
m_FileWorkerOptions.EmailProperties.Message = message
m_SmtpClient.QueueMail(m_FileWorkerOptions.EmailProperties)
End If
Dim records As StreamReader = _
My.Computer.FileSystem.OpenTextFileReader(TextFile)
Dim record As String
record = records.ReadLine()
While (Not record Is Nothing)
Try
LinqSql.InsertRecord(record)
Catch ex As Exception
WriteLogEvent(My.Resources.ThreadIOError + _
"_" + ex.ToString + "_" + Now.ToString, THREAD_ERROR, _
EventLogEntryType.Error, My.Resources.Source)
End Try
record = records.ReadLine
End While
records.Close()
records.Dispose()
My.Computer.FileSystem.MoveFile(TextFile, ProcessFile, True)
System.Threading.Thread.Sleep(0)
WriteLogEvent(My.Resources.ThreadMessage + _
TextFile, THREAD_INFO, EventLogEntryType.Information, _
My.Resources.Source)
Catch ex As Exception
WriteLogEvent(My.Resources.ThreadErrorMessage + _
"_" + ex.ToString + "_" + Now.ToString, THREAD_ERROR, _
EventLogEntryType.Error, My.Resources.Source)
End Try
Next
Catch fio As IOException
WriteLogEvent(My.Resources.ThreadIOError + "_" + fio.ToString _
+ "_" + Now.ToString, THREAD_ABORT_ERROR, _
EventLogEntryType.Error, My.Resources.Source)
Catch tab As ThreadAbortException
'this must be listed first as Exception is the master catch
'Clean up thread here
WriteLogEvent(My.Resources.ThreadAbortMessage + "_" _
+ tab.ToString + "_" + Now.ToString, THREAD_ABORT_ERROR, _
EventLogEntryType.Error, My.Resources.Source)
Catch ex As Exception
WriteLogEvent(My.Resources.ThreadErrorMessage + "_" + _
ex.ToString + "_" + Now.ToString, THREAD_ERROR, _
EventLogEntryType.Error, My.Resources.Source)
End Try
End If
If Not m_ThreadAction.StopThread Then
Thread.Sleep(THREAD_WAIT)
End If
End While
End Sub


The bolded code in Listing 7-6 creates an instance of our LINQSQL class before we enter our thread loop because we only need one instance of this class.

When a file is found, we open the file and iterate through it, passing each line to our <InsertRecord> method on the LINQSQL instance. This will populate the database with a new User instance. If the insertion fails, we log an event and move the file.

Install and Verify

To validate that the service can read files and create records for the system to insert, I have modified my configuration.xml file so that it only points to the incoming folder and looks for *.txt files.

I have created three files with five records each. Listing 2 is a sample from one of the text files.

Listing 2. Demonstration data used for service verification.
FirstName,LastName,111 South Street,NA,TestCity,TT,00000,555-1212
FirstName2,LastName,111 South Street,NA,TestCity,TT,00000,555-1212
FirstName3,LastName,111 South Street,NA,TestCity,TT,00000,555-1212
FirstName4,LastName,111 South Street,NA,TestCity,TT,00000,555-1212
FirstName5,LastName,111 South Street,NA,TestCity,TT,00000,555-1212

For each file I added five records. Because there are no unique index constraints on any columns but the UniqueIdentifier field, or GUID, the data shown in Listing 2 is appropriate.

You can run these files through as many times as you like by merely cutting and pasting them back into your incoming folder each time they are processed successfully.

Other -----------------
- Microsoft SQL Server 2008 R2 : Setting Up Replication (part 4) - Creating Subscriptions
- Microsoft SQL Server 2008 R2 : Setting Up Replication (part 3) - Horizontal and Vertical Filtering
- Microsoft SQL Server 2008 R2 : Setting Up Replication (part 2) - Creating a Publication
- Microsoft SQL Server 2008 R2 : Setting Up Replication (part 1) - Creating a Distributor and Enabling Publishing
- SQL Server 2008 R2 : Basing the Replication Design on User Requirements
- SQL Server 2008 R2 : Planning for SQL Server Data Replication & SQL Server Replication Types
- SQL Server 2008 R2 : Replication Agents
- SQL Server 2008 : Replication - Subscriptions
- SQL Server 2008 : Replication Scenarios
- Protecting SQL Server Data : CELL-LEVEL ENCRYPTION - Special Considerations
- Protecting SQL Server Data : SCHEMA ARCHITECTURE STRATEGIES - Harnessing Linked Servers
- Monitoring SQL Server 2005 Performance : Using Windows System Monitor & Using SQL Server Profiler
- Monitoring SQL Server 2005 Performance : Monitoring and Recording Performance
- SQL Server 2008 R2 : Replication - The Publisher, Distributor, and Subscriber Magazine Metaphor
- SQL Server 2008 R2 : Replication - What Is Replication?
- SQL Server 2008 High Availability : Other HA Techniques That Yield Great Results & High Availability from the Windows Server Family Side
- SQL Server 2008 High Availability : Building Solutions with One or More HA Options
- SQL Server 2008 High Availability : The Fundamentals of HA
- Administering SQL Server 2008 with PowerShell : Step-By-Step Examples (part 4)
- Administering SQL Server 2008 with PowerShell : Step-By-Step Examples (part 3) - Performing a Database Backup
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us